<?php
include_once $_SERVER['DOCUMENT_ROOT']."/math-videos/configure.inc.php";
//$_SESSION['user']=$_POST['username'];

//$_SESSION['user_id'] = $user_id;

//$_SESSION['admin'] = $admin_flag;

//echo $_SESSION['user_id']."<br>";
//echo "!!!!!!!!".$_SESSION['admin']."<br>";

if($_GET['page']!='')
{
	$cur_page = (int)$_GET['page'];
}
$limit=30;
$heading_area = $null;
$heading_area .= "<div style=\"font-family:Arial, Verdana, sans-serif; font-size : 12px;width:100%;margin:0px;padding-bottom:1px;\">";

$heading_area .= "<TABLE CELLPADDING=2 CELLSPACING=0 style=\"font-family:Arial, Verdana, sans-serif; font-size : 12px; color:white; background-color:#0099CC;width:100%;\" >";
$heading_area .= "<tr >
										<td colspan=2  align=\"right\" style=\"padding-right:20px;font-weight:bold;font-size : 12px;\"> <a href='logout.php' style=\"color:#fff;text-decoration:none;\" >Logout</a>
										</td>
									</tr>";  
$heading_area .= "<tr >
										<td colspan=2  align=\"center\" width=\"100%\" style=\"font-size:26px;\"> Math Video Maintenance
										</td>
									</tr>";  
// put out blue row to hold action buttons (add, cancel, delete, etc.)
$heading_area .= "
		<tr>
			<td colspan=2 align=\"center\" ><a href='../admin/modules/maintenance.php' style=\"color:#fff;font-size : 12px;font-weight:bold;\">Add New Asset</a>
			&nbsp;&nbsp;<a href='../admin/error_log_report.php' style=\"color:#fff;font-size : 12px;font-weight:bold;\">Error logs</a>
			&nbsp;&nbsp;<a href='../admin/api_log.php' style=\"color:#fff;font-size : 12px;font-weight:bold;\">API logs</a>";
$heading_area .= "</td></tr>";

$heading_area .= "
	</TABLE></div>";
echo $heading_area;
echo "<div align=\"center\">";
if($_SESSION['admin'])
{
	$sqltext="select asset.id,
									asset.title,
									asset.abstract, 
									to_char(asset.lecture_date,'mm/dd/yyyy') as lecture_date, 
									asset.duration, 
								  asset.asset_type, 
									asset.abstract_url, 
									asset.remote_url, 
									asset.location, 
									asset.event_title, 
									to_char(asset.created_datetime,'mm/dd/yyyy') as created_datetime, 
									asset.institute_id,
									asset.created_by, 
									asset.rating, 
									asset.views, 
									asset.thumbnail,
								  (select presenter.first_name ||' '|| presenter.last_name as presenter_name
										from presenter WHERE presenter.asset_id=asset.id) as presenter_name
									from asset
	order by institute_id,id";
	$params = array();
}
else
{
	$sqltext2="select institute_id from users  WHERE id=?";
  $params2 = array();
  $institute_id='';
  array_push($params2,sql_escape($_SESSION['user_id']));
  $result2 = db_select_query($conn2,$sqltext2,$params2);
 
  while($row2 = db_fetch_array($result2))
  {
	  $institute_id = $row2['institute_id'];
  }
  $sqltext="select asset.id,
									asset.title, 
									asset.abstract, 
									to_char(asset.lecture_date,'mm/dd/yyyy') as lecture_date, 
									asset.duration, 
								  asset.asset_type, 
									asset.abstract_url, 
									asset.remote_url, 
									asset.location, 
									asset.event_title,
									to_char(asset.created_datetime,'mm/dd/yyyy') as created_datetime, 
									asset.created_by, 									
									asset.rating, 
									asset.views, 
									asset.thumbnail,
									asset.institute_id,
								 (select presenter.first_name ||' '|| presenter.last_name as presenter_name
										from presenter WHERE presenter.asset_id=asset.id) as presenter_name
	from asset 
	WHERE institute_id =?
	order by institute_id, id DESC";
	$params = array();	
	array_push($params,sql_escape($institute_id));
}
//echo $sqltext."<br>";
//list all the asset information
	$result = db_select_query($conn,$sqltext,$params);
	unset($array_list);
	while($row = db_fetch_object($result))
	{
		$array_list[]=$row;
		
	}
	$name ='';
	$pre_institute_id='';
	$items_num=count($array_list);
	$limitvalue=0;
  if($items_num>$limit)	{
		$pagename = "index.php?";		
		if($cur_page !='')
		{
			$limitvalue = $limit*($cur_page-1);
		}		
	}
	$num_per_page=0;
	echo "<div align='right' style='padding-right:10px;' >";
				paginate($items_num,$limit,$cur_page,"#336699");
				echo "</div>";
	for($i=$limitvalue; $i<count($array_list);$i++)
	{
		
		foreach($array_list[$i] as $key => $value) 
		{
			$value = trim($value);
			$$key = $value;			
		}
		$search_num =0;
		$click_num =0;
		if($id !='')
		{
			$sqltext4="select count(*) as search_num from search_result  WHERE asset_id=?";
			$params4 = array();			
			array_push($params4,sql_escape($id));
			$result4 = db_select_query($conn,$sqltext4,$params4);
			while($row4 = db_fetch_array($result4))
			{
				$search_num = $row4['search_num'];
			}
			$sqltext4="select count(*) as click_num from view_result  WHERE asset_id=?";
			$params4 = array();			
			array_push($params4,sql_escape($id));
			$result4 = db_select_query($conn,$sqltext4,$params4);
			while($row4 = db_fetch_array($result4))
			{
				$click_num = $row4['click_num'];
			}
		}
		if($name =='')
		{
			$sqltext3="select full_name from institutes  WHERE id=?";
			$params3 = array();			
			array_push($params3,sql_escape($institute_id));
			$result3 = db_select_query($conn2,$sqltext3,$params3);
			while($row3 = db_fetch_array($result3))
			{
				$name = $row3['full_name'];
			}
			echo "<div align='left' style='font-size:14px;padding-left:10px;'><b>".$name."</b>:</div>";
			echo "<table cellspacing='1' cellpadding='3' style='background-color:#ccc;margin:10px;width:99%;'>";
			echo "<tr><th align='left'>Title - Presenter - Date</th><th align='left'>Searched Times</th><th align='left'>Clicked Times</th><th>&nbsp;</th><th>&nbsp;</th><th>&nbsp;</th></tr>";
			$pre_institute_id = $institute_id;
		}
		elseif($pre_institute_id != $institute_id)
		{
			$sqltext3="select full_name from institutes  WHERE id=?";
			$params3 = array();			
			array_push($params3,sql_escape($institute_id));
			$result3 = db_select_query($conn2,$sqltext3,$params3);
			while($row3 = db_fetch_array($result3))
			{
				$name = $row3['full_name'];
			}
			echo "</table ><br />";
			echo "<div align='left' style='font-size:14px;padding-left:10px;'><b>".$name."</b>:</div>";
			echo "<table cellspacing='1' cellpadding='3' style='background-color:#ccc;margin:10px;width:100%;'>";
			echo "<tr><th align='left'>Title - Presenter - Date</th><th align='left'>Searched Times</th><th align='left'>Clicked Times</th><th>&nbsp;</th><th>&nbsp;</th><th>&nbsp;</th></tr>";
	
			$pre_institute_id = $institute_id;
		}
		echo "<tr bgcolor='#ffffff'><td><a href='../admin/modules/maintenance.php?asset_id=$id'>".$title." - by ". $presenter_name." - " .$lecture_date."</a></td>";
		echo "<td>$search_num</td>";
		echo "<td>$click_num</td>";
		 echo "<td><a href='../admin/modules/maintenance.php?asset_id=$id' >Edit</a></td>";
		 echo "<td><a href='../admin/index.php?id=$id&amp;module=view' target='_blank'>View Api Info</a></td>";
		echo "<td align='center' style='width:120px;'><a href='../admin/index.php?id=$id&amp;del=Y&amp;module=edit' onclick=\"confirm('Do you really want to delete this asset?')\">Delete</a></td></tr>";
		$num_per_page++;
		if($num_per_page >=$limit)
		{
			break;
		}
	}
echo "</table>";
echo "</div>"; 
	?>
